package top.infopub.mgr.la.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.la.dao.PeaceLawcaseAttDao;
import top.infopub.mgr.la.dao.PeaceLawcaseDao;
import top.infopub.mgr.la.domain.PeaceLawcaseDomain;
import top.infopub.mgr.la.service.PeaceLawcaseService;
import top.infopub.mgr.la.vo.PeaceLawcaseVo;
import top.infopub.model.la.PeaceLawcase;
import top.infopub.model.la.PeaceLawcaseAtt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class PeaceLawcaseServiceImpl extends GenericServiceImpl<PeaceLawcase, String> implements
		PeaceLawcaseService {

	@Autowired
	private PeaceLawcaseDao peaceLawcaseDao;
	
	@Autowired
    private PeaceLawcaseAttDao peaceLawcaseAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<PeaceLawcase, String> getDao() {
		return peaceLawcaseDao;
	}

    @Override
    public Page<PeaceLawcaseDomain> selectByDomainWhere(Page<PeaceLawcaseDomain> page,
                                                        PeaceLawcaseVo peaceLawcaseVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PeaceLawcaseDomain>)peaceLawcaseDao.selectByDomainWhere(peaceLawcaseVo);
    }

    @Override
    public Page<PeaceLawcaseDomain> selectMyCheckByDomainWhere(Page<PeaceLawcaseDomain> page,
                                                               PeaceLawcaseVo peaceLawcaseVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PeaceLawcaseDomain>)peaceLawcaseDao.selectMyCheckByDomainWhere(peaceLawcaseVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePeaceLawcase(PeaceLawcaseVo peaceLawcase,
                                 List<PeaceLawcaseAtt> peaceLawcaseAtts) {
        peaceLawcaseDao.insertById(peaceLawcase);
        
        if(CollectionUtils.isNotEmpty(peaceLawcaseAtts)){
            peaceLawcaseAttDao.insertByBatch(peaceLawcaseAtts);
        }
        
        // 1000万
        BigDecimal cmp = new BigDecimal("10000000");
        String srcBizTable = "LA_PEACE_LAWCASE";
        String branchCode_lt = "target_amount<=1000万";
        String branchCode_gt = "target_amount>1000万";
        String templateId = "";
        // <=1000万
        if (peaceLawcase.getTargetAmount().compareTo(cmp) <= 0) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_lt);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_gt);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, peaceLawcase.getId(),
            peaceLawcase.getProcessId(), null);
    }

    @Override
    public PeaceLawcaseVo queryById(String id) {
        return peaceLawcaseDao.selectById(id);
    }
}